package com.jmgd.cjlx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jmgd.cjlx.dao.*;
import com.jmgd.cjlx.entity.*;
import com.jmgd.cjlx.param.metuan.*;
import com.jmgd.cjlx.service.*;
import com.sankuai.meituan.shangou.open.sdk.domain.SystemParam;
import com.sankuai.meituan.shangou.open.sdk.exception.SgOpenException;
import com.sankuai.meituan.shangou.open.sdk.request.*;
import com.sankuai.meituan.shangou.open.sdk.response.SgOpenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;


@Service
public class MeTuanServiceImpl implements MeTuanService {
    @Autowired
    TMeituanWareMapper tMeituanWareMapper;

    @Autowired
    TMeituanWareService tMeituanWareService;

    @Autowired
    TMeituanMdMapper tMeituanMdMapper;

    @Autowired
    ActRetailDiscountService actRetailDiscountService;

    @Autowired
    ActRetailDiscountOtherService actRetailDiscountOtherService;

    @Autowired
    ActRetailDiscountMapper actRetailDiscountMapper;

    @Autowired
    TMeituanMdOtherMapper tMeituanMdOtherMapper;

    @Autowired
    TMeituanWareOtherMapper tMeituanWareOtherMapper;

    @Autowired
    TMeituanWareOtherService tMeituanWareOtherService;

    @Autowired
    ActRetailDiscountOtherMapper actRetailDiscountOtherMapper;

    public static void main(String[] args) throws IOException {
        //gwMedicineList();

       // gwMedicineget();

        //poiGetids();

      //  poimget();

      //  actAllGetByAppPoiCodeAndType();

    }



    @Override
    public void gwMedicineList() {
      //  List<String> poiGetidList = poiGetids();

        List<String> poiGetidList = tMeituanMdMapper.select1();

 //       List<String> poiGetidList = new ArrayList<>();

//        poiGetidList.add("101888");
//
//        tMeituanWareMapper.delete(null);
        for(String busno:poiGetidList) {
        int offset =1;
        int limit = 200;
        List<TMeituanWare> tMeituanWareList = new ArrayList<>();
        int total = gwMedicineListnb(busno,Integer.toString(offset),
                                    Integer.toString(limit),tMeituanWareList
                );
        System.out.println(total);

        int count = (int) Math.ceil(total/limit);

        for(int i = 1;i<=count;i++){
            System.out.println("======================"+i);
            offset = offset + limit;
            gwMedicineListnb(busno,Integer.toString(offset),Integer.toString(limit),tMeituanWareList);
        }

            QueryWrapper<TMeituanWare> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shopno",busno);
            tMeituanWareMapper.delete(queryWrapper);
        tMeituanWareService.saveBatch(tMeituanWareList);

        }

        // 获取当前日期的年月日
        LocalDate today = LocalDate.now();

        // 将 LocalDate 转换为 Date 类型，因为数据库通常用 Date 处理
        Date startOfToday = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());

        QueryWrapper<TMeituanWare> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("createdate", startOfToday);
        tMeituanWareMapper.delete(queryWrapper);
    }


    @Override
    public void gwMedicineListBusno() {
        List<String> poiGetidList = tMeituanWareMapper.selectTMeituanWareBusno();
//
//        tMeituanWareMapper.delete(null);
        for(String busno:poiGetidList) {
            int offset =1;
            int limit = 200;
            List<TMeituanWare> tMeituanWareList = new ArrayList<>();
            int total = gwMedicineListnb(busno,Integer.toString(offset),
                    Integer.toString(limit),tMeituanWareList
            );
            System.out.println(total);

            int count = (int) Math.ceil(total/limit);

            for(int i = 1;i<=count;i++){
                System.out.println("======================"+i);
                offset = offset + limit;
                gwMedicineListnb(busno,Integer.toString(offset),Integer.toString(limit),tMeituanWareList);
            }

            QueryWrapper<TMeituanWare> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shopno",busno);
            tMeituanWareMapper.delete(queryWrapper);
            tMeituanWareService.saveBatch(tMeituanWareList);

        }
    }

    public int gwMedicineListnb(String busno,String offset,String limit,List<TMeituanWare> tMeituanWareList) {
        SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
        String uri = "/api/v1/gw/medicine/list";
        String requestMethodType = "GET";
        SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

        Map<String, String> paramMap = new HashMap();
        paramMap.put("app_poi_code", busno);
        paramMap.put("offset", offset);
        paramMap.put("limit", limit);
        sgOpenGeneralRequest.setParamMap(paramMap);

        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = sgOpenGeneralRequest.doRequest();

            String baseString = sgOpenResponse.getBaseString();
//            System.out.println("1" + baseString);
            String requestSig = sgOpenResponse.getRequestSig();
//            System.out.println("2" + requestSig);
            String requestResult = sgOpenResponse.getRequestResult();
//            System.out.println("3" + requestResult);

            ObjectMapper objectMapper = new ObjectMapper();
                MedicineResponse response = objectMapper.readValue(requestResult, MedicineResponse.class);
//                System.out.println("Code: " + response.getCode());
//                System.out.println("Message: " + response.getMsg());
//                System.out.println("Total Hits: " + response.getData().getTotal());

                for (MedicineResponse.Data.Hit hit : response.getData().getHits()) {
//                    System.out.println("Medicine Name: " + hit.getName());
                    TMeituanWare tMeituanWare = new TMeituanWare();
                    tMeituanWare.setAppmedicinecode(hit.getAppMedicineCode());
                    tMeituanWare.setCategorycode(hit.getCategoryCode());
                    tMeituanWare.setCategoryname(hit.getCategoryName());
                    //tMeituanWare.setCtime(hit.getCtime());
                    tMeituanWare.setIssoldout(hit.getIsSoldOut());
                    tMeituanWare.setMedicineno(hit.getMedicineNo());
                    tMeituanWare.setMedicinetype(hit.getMedicineType());
                    tMeituanWare.setTname(hit.getName());


                    tMeituanWare.setSequence(hit.getSequence());
                    tMeituanWare.setShopno(busno);
                   // tMeituanWare.setUtime(hit.getUtime());
//                    tMeituanWare.setDocument();
//                    tMeituanWare.setParent();
//                    tMeituanWare.setText();
                    tMeituanWare.setTagid(hit.getTagId());


                    List<Sku> skus = hit.getSkusList();
                    if (skus != null) {
                        for (Sku sku : skus) {
                            tMeituanWare.setPrice(sku.getPrice());

                            tMeituanWare.setSkuid(sku.getSkuId());
                            tMeituanWare.setSpec(sku.getSpec());
                            tMeituanWare.setStock(sku.getStock());

                            tMeituanWare.setUpc(sku.getUpc());
                            tMeituanWare.setIssellflag(sku.getIsSellFlag());

//                            System.out.println("SKU ID: " + sku.getSkuId());
//                            System.out.println("Spec: " + sku.getSpec());
//                            System.out.println("UPC: " + sku.getUpc());
//                            System.out.println("Stock: " + sku.getStock());
//                            System.out.println("Price: " + sku.getPrice());
//                            System.out.println("Location Code: " + sku.getLocationCode());
//                            System.out.println("Is Sell Flag: " + sku.getIsSellFlag());
                        }
                    }
                    tMeituanWareList.add(tMeituanWare);
                }
                return response.getData().getTotal();
        } catch (SgOpenException var8) {
            var8.printStackTrace();
        } catch (IOException var9) {
            var9.printStackTrace();
        }
        return 0;
    }


    public int gwMedicineListnbOther(String busno,String offset,String limit,List<TMeituanWareOther> tMeituanWareList) {
        SystemParam systemParam = new SystemParam("122042", "c8c58659a05616b678d29e9765eda629");
        String uri = "/api/v1/gw/medicine/list";
        String requestMethodType = "GET";
        SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

        Map<String, String> paramMap = new HashMap();
        paramMap.put("app_poi_code", busno);
        paramMap.put("offset", offset);
        paramMap.put("limit", limit);
        sgOpenGeneralRequest.setParamMap(paramMap);

        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = sgOpenGeneralRequest.doRequest();

            String baseString = sgOpenResponse.getBaseString();
//            System.out.println("1" + baseString);
            String requestSig = sgOpenResponse.getRequestSig();
//            System.out.println("2" + requestSig);
            String requestResult = sgOpenResponse.getRequestResult();
//            System.out.println("3" + requestResult);

            ObjectMapper objectMapper = new ObjectMapper();
            MedicineResponse response = objectMapper.readValue(requestResult, MedicineResponse.class);
//                System.out.println("Code: " + response.getCode());
//                System.out.println("Message: " + response.getMsg());
//                System.out.println("Total Hits: " + response.getData().getTotal());

            for (MedicineResponse.Data.Hit hit : response.getData().getHits()) {
//                    System.out.println("Medicine Name: " + hit.getName());
                TMeituanWareOther tMeituanWare = new TMeituanWareOther();
                tMeituanWare.setAppmedicinecode(hit.getAppMedicineCode());
                tMeituanWare.setCategorycode(hit.getCategoryCode());
                tMeituanWare.setCategoryname(hit.getCategoryName());
                //tMeituanWare.setCtime(hit.getCtime());
                tMeituanWare.setIssoldout(hit.getIsSoldOut());
                tMeituanWare.setMedicineno(hit.getMedicineNo());
                tMeituanWare.setMedicinetype(hit.getMedicineType());
                tMeituanWare.setTname(hit.getName());


                tMeituanWare.setSequence(hit.getSequence());
                tMeituanWare.setShopno(busno);
                // tMeituanWare.setUtime(hit.getUtime());
//                    tMeituanWare.setDocument();
//                    tMeituanWare.setParent();
//                    tMeituanWare.setText();
                tMeituanWare.setTagid(hit.getTagId());


                List<Sku> skus = hit.getSkusList();
                if (skus != null) {
                    for (Sku sku : skus) {
                        tMeituanWare.setPrice(sku.getPrice());

                        tMeituanWare.setSkuid(sku.getSkuId());
                        tMeituanWare.setSpec(sku.getSpec());
                        tMeituanWare.setStock(sku.getStock());

                        tMeituanWare.setUpc(sku.getUpc());
                        tMeituanWare.setIssellflag(sku.getIsSellFlag());

//                            System.out.println("SKU ID: " + sku.getSkuId());
//                            System.out.println("Spec: " + sku.getSpec());
//                            System.out.println("UPC: " + sku.getUpc());
//                            System.out.println("Stock: " + sku.getStock());
//                            System.out.println("Price: " + sku.getPrice());
//                            System.out.println("Location Code: " + sku.getLocationCode());
//                            System.out.println("Is Sell Flag: " + sku.getIsSellFlag());
                    }
                }
                tMeituanWareList.add(tMeituanWare);
            }
            return response.getData().getTotal();
        } catch (SgOpenException var8) {
            var8.printStackTrace();
        } catch (IOException var9) {
            var9.printStackTrace();
        }
        return 0;
    }



    public static void gwMedicineget() {
        SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
        String uri = "/api/v1/gw/medicine/get";
        String requestMethodType = "GET";
        SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

        Map<String, String> paramMap = new HashMap();
        paramMap.put("app_poi_code", "101444");
        paramMap.put("app_medicine_code", "2010834");

        sgOpenGeneralRequest.setParamMap(paramMap);


        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = sgOpenGeneralRequest.doRequest();
        } catch (SgOpenException var8) {
            var8.printStackTrace();
            return;
        } catch (IOException var9) {
            var9.printStackTrace();
            return;
        }

        String baseString = sgOpenResponse.getBaseString();
        System.out.println(baseString);
        String requestSig = sgOpenResponse.getRequestSig();
        System.out.println(requestSig);
        String requestResult = sgOpenResponse.getRequestResult();
        System.out.println(requestResult);
    }


    //获取门店id
   public List<String> poiGetids(){
       SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
       String uri = "/api/v1/poi/getids";
       String requestMethodType = "GET";
       SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

       SgOpenResponse sgOpenResponse;
       try {
           sgOpenResponse = sgOpenGeneralRequest.doRequest();
           String baseString = sgOpenResponse.getBaseString();
           System.out.println(baseString);
           String requestSig = sgOpenResponse.getRequestSig();
           System.out.println(requestSig);
           String requestResult = sgOpenResponse.getRequestResult();
           System.out.println(requestResult);

           ObjectMapper objectMapper = new ObjectMapper();

           // 将JSON字符串解析为Response对象
           Busno response = objectMapper.readValue(requestResult, Busno.class);

           // 输出解析结果
           System.out.println("Result Code: " + response.getResultCode());
           System.out.println("Data:");
           System.out.println(response.getSuccessList().size());
           System.out.println(response.getData().size());
           for (String data : response.getData()) {
               System.out.println(data);
           }
           return response.getSuccessList();
       } catch (SgOpenException var8) {
           var8.printStackTrace();
       } catch (IOException var9) {
           var9.printStackTrace();
       }
        return null;
   }

   //获取门店详细资料
    @Override
    public void poimget() throws InterruptedException {
        List<String> poiGetidList = poiGetids();

        for(String busno:poiGetidList) {
//批量获取门店详细信息
            SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
            String uri = "/api/v1/poi/mget";
            String requestMethodType = "GET";
            SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

            Map<String, String> paramMap = new HashMap();
            paramMap.put("app_poi_codes", busno);

            sgOpenGeneralRequest.setParamMap(paramMap);


            SgOpenResponse sgOpenResponse;
            try {
                sgOpenResponse = sgOpenGeneralRequest.doRequest();
            } catch (SgOpenException var8) {
                var8.printStackTrace();
                return;
            } catch (IOException var9) {
                var9.printStackTrace();
                return;
            }

            String baseString = sgOpenResponse.getBaseString();
            System.out.println(baseString);
            String requestSig = sgOpenResponse.getRequestSig();
            System.out.println(requestSig);
            String requestResult = sgOpenResponse.getRequestResult();
            System.out.println(requestResult);


            //获取映射
            SystemParam systemParam2 = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
            String uri2 = "/api/v1/ecommerce/poi/bound/list";
            String requestMethodType2 = "GET";
            SgOpenGeneralRequest sgOpenGeneralRequest2 = new SgOpenGeneralRequest(uri2, requestMethodType2, systemParam2);

            Map<String, String> paramMap2 = new HashMap();
            paramMap2.put("app_poi_code", busno);
            paramMap2.put("page_num", "1");
            paramMap2.put("page_size", "200");

            sgOpenGeneralRequest2.setParamMap(paramMap2);


            SgOpenResponse sgOpenResponse2;
            try {
                sgOpenResponse2 = sgOpenGeneralRequest2.doRequest();
            } catch (SgOpenException var8) {
                var8.printStackTrace();
                return;
            } catch (IOException var9) {
                var9.printStackTrace();
                return;
            }

            String baseString2 = sgOpenResponse2.getBaseString();
            //System.out.println(baseString2);
            String requestSig2 = sgOpenResponse2.getRequestSig();
            //System.out.println(requestSig2);
            String requestResult2 = sgOpenResponse2.getRequestResult();
            System.out.println("=============================");
            System.out.println(requestResult2);
            System.out.println("=============================");

            ObjectMapper objectMapper2 = new ObjectMapper();

            ObjectMapper objectMapper = new ObjectMapper();
            try {
                BusnoListResponse response = objectMapper.readValue(requestResult, BusnoListResponse.class);

                List<BusnoDetail> busnoDetailList = response.getSuccessList();

                for (BusnoDetail busnoDetail : busnoDetailList) {
                    TMeituanMd tMeituanMd = new TMeituanMd();
                    tMeituanMd.setAddress(busnoDetail.getAddress());
                    tMeituanMd.setAppid(busnoDetail.getAppId());
                    tMeituanMd.setApppoicode(busnoDetail.getAppPoiCode());
                    tMeituanMd.setCityid(busnoDetail.getCityId());
                    tMeituanMd.setCtime(busnoDetail.getCtime());
                    tMeituanMd.setInvoicedescription(busnoDetail.getInvoiceDescription());
                    tMeituanMd.setInvoiceminprice(busnoDetail.getInvoiceMinPrice());
                    tMeituanMd.setInvoicesupport(busnoDetail.getInvoiceSupport());
                    tMeituanMd.setIsonline(busnoDetail.getIsOnline());
                    //tMeituanMd.setLatitude(busnoDetail.getLatitude());
                    tMeituanMd.setLocationid(busnoDetail.getLocationId());
                    tMeituanMd.setLogisticscodes(busnoDetail.getLogisticsCodes());
                    // tMeituanMd.setLongitude(busnoDetail.getLatitude());
                    tMeituanMd.setName(busnoDetail.getName());
                    tMeituanMd.setOpenlevel(busnoDetail.getOpenLevel());
                    tMeituanMd.setPhone(busnoDetail.getPhone());
                    tMeituanMd.setPicurl(busnoDetail.getPicUrl());
                    tMeituanMd.setPrebook(busnoDetail.getPreBook());
                    tMeituanMd.setPrebookmaxdays(busnoDetail.getPreBookMaxDays());
                    tMeituanMd.setPrebookmindays(busnoDetail.getPreBookMinDays());
                    tMeituanMd.setPromotioninfo(busnoDetail.getPromotionInfo());
                    tMeituanMd.setRemark(busnoDetail.getRemark());
                    tMeituanMd.setShippingfee(busnoDetail.getShippingFee());
                    tMeituanMd.setShippingtime(busnoDetail.getShippingTime());
                    tMeituanMd.setStandbytel(busnoDetail.getStandbyTel());
                    tMeituanMd.setTagname(busnoDetail.getTagName());
                    tMeituanMd.setTimeselect(busnoDetail.getTimeSelect());
                    tMeituanMd.setUtime(busnoDetail.getUtime());

                    PoiboundResult result2 = objectMapper2.readValue(requestResult2, PoiboundResult.class);
                    // 现在你可以访问解析后的数据了
                    System.out.println("Result code: " + result2.getResultCode());
                    System.out.println("Total count in extra_info: " + result2.getExtraInfo().getTotalCount());
                    System.out.println("First data poi_name: " + result2.getSuccessList().get(0).getWmPoiId());

                    tMeituanMd.setWmpoiid(String.valueOf(result2.getSuccessList().get(0).getWmPoiId()));

                    QueryWrapper<TMeituanMd> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("apppoicode",busnoDetail.getAppPoiCode());
                    tMeituanMdMapper.delete(queryWrapper);

                    tMeituanMdMapper.insert(tMeituanMd);
                    System.out.println("解析成功！");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            Thread.sleep(500);
        }
    }




    //获取门店详细资料
    @Override
    public void poimgetOther() throws InterruptedException {
        //List<String> poiGetidList = poiGetidsOther();
        List<String> poiGetidList = tMeituanMdOtherMapper.select1();

        for(String busno:poiGetidList) {
//批量获取门店详细信息
            SystemParam systemParam = new SystemParam("122042", "c8c58659a05616b678d29e9765eda629");
            String uri = "/api/v1/poi/mget";
            String requestMethodType = "GET";
            SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

            Map<String, String> paramMap = new HashMap();
            paramMap.put("app_poi_codes", busno);

            sgOpenGeneralRequest.setParamMap(paramMap);


            SgOpenResponse sgOpenResponse;
            try {
                sgOpenResponse = sgOpenGeneralRequest.doRequest();
            } catch (SgOpenException var8) {
                var8.printStackTrace();
                return;
            } catch (IOException var9) {
                var9.printStackTrace();
                return;
            }

            String baseString = sgOpenResponse.getBaseString();
            System.out.println(baseString);
            String requestSig = sgOpenResponse.getRequestSig();
            System.out.println(requestSig);
            String requestResult = sgOpenResponse.getRequestResult();
            System.out.println(requestResult);


            //获取映射
            SystemParam systemParam2 = new SystemParam("122042", "c8c58659a05616b678d29e9765eda629");
            String uri2 = "/api/v1/ecommerce/poi/bound/list";
            String requestMethodType2 = "GET";
            SgOpenGeneralRequest sgOpenGeneralRequest2 = new SgOpenGeneralRequest(uri2, requestMethodType2, systemParam2);

            Map<String, String> paramMap2 = new HashMap();
            paramMap2.put("app_poi_code", busno);
            paramMap2.put("page_num", "1");
            paramMap2.put("page_size", "200");

            sgOpenGeneralRequest2.setParamMap(paramMap2);


            SgOpenResponse sgOpenResponse2;
            try {
                sgOpenResponse2 = sgOpenGeneralRequest2.doRequest();
            } catch (SgOpenException var8) {
                var8.printStackTrace();
                return;
            } catch (IOException var9) {
                var9.printStackTrace();
                return;
            }

            String baseString2 = sgOpenResponse2.getBaseString();
            //System.out.println(baseString2);
            String requestSig2 = sgOpenResponse2.getRequestSig();
            //System.out.println(requestSig2);
            String requestResult2 = sgOpenResponse2.getRequestResult();
            System.out.println("=============================");
            System.out.println(requestResult2);
            System.out.println("=============================");

            ObjectMapper objectMapper2 = new ObjectMapper();

            ObjectMapper objectMapper = new ObjectMapper();
            try {
                BusnoListResponse response = objectMapper.readValue(requestResult, BusnoListResponse.class);

                List<BusnoDetail> busnoDetailList = response.getSuccessList();

                for (BusnoDetail busnoDetail : busnoDetailList) {
                    TMeituanMdOther tMeituanMd = new TMeituanMdOther();
                    tMeituanMd.setAddress(busnoDetail.getAddress());
                    tMeituanMd.setAppid(busnoDetail.getAppId());
                    tMeituanMd.setApppoicode(busnoDetail.getAppPoiCode());
                    tMeituanMd.setCityid(busnoDetail.getCityId());
                    tMeituanMd.setCtime(busnoDetail.getCtime());
                    tMeituanMd.setInvoicedescription(busnoDetail.getInvoiceDescription());
                    tMeituanMd.setInvoiceminprice(busnoDetail.getInvoiceMinPrice());
                    tMeituanMd.setInvoicesupport(busnoDetail.getInvoiceSupport());
                    tMeituanMd.setIsonline(busnoDetail.getIsOnline());
                    //tMeituanMd.setLatitude(busnoDetail.getLatitude());
                    tMeituanMd.setLocationid(busnoDetail.getLocationId());
                    tMeituanMd.setLogisticscodes(busnoDetail.getLogisticsCodes());
                    // tMeituanMd.setLongitude(busnoDetail.getLatitude());
                    tMeituanMd.setName(busnoDetail.getName());
                    tMeituanMd.setOpenlevel(busnoDetail.getOpenLevel());
                    tMeituanMd.setPhone(busnoDetail.getPhone());
                    tMeituanMd.setPicurl(busnoDetail.getPicUrl());
                    tMeituanMd.setPrebook(busnoDetail.getPreBook());
                    tMeituanMd.setPrebookmaxdays(busnoDetail.getPreBookMaxDays());
                    tMeituanMd.setPrebookmindays(busnoDetail.getPreBookMinDays());
                    tMeituanMd.setPromotioninfo(busnoDetail.getPromotionInfo());
                    tMeituanMd.setRemark(busnoDetail.getRemark());
                    tMeituanMd.setShippingfee(busnoDetail.getShippingFee());
                    tMeituanMd.setShippingtime(busnoDetail.getShippingTime());
                    tMeituanMd.setStandbytel(busnoDetail.getStandbyTel());
                    tMeituanMd.setTagname(busnoDetail.getTagName());
                    tMeituanMd.setTimeselect(busnoDetail.getTimeSelect());
                    tMeituanMd.setUtime(busnoDetail.getUtime());

                    PoiboundResult result2 = objectMapper2.readValue(requestResult2, PoiboundResult.class);
                    // 现在你可以访问解析后的数据了
                    System.out.println("Result code: " + result2.getResultCode());
                    System.out.println("Total count in extra_info: " + result2.getExtraInfo().getTotalCount());
                    System.out.println("First data poi_name: " + result2.getSuccessList().get(0).getWmPoiId());

                    tMeituanMd.setWmpoiid(String.valueOf(result2.getSuccessList().get(0).getWmPoiId()));

                    QueryWrapper<TMeituanMdOther> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("apppoicode",busnoDetail.getAppPoiCode());
                    tMeituanMdOtherMapper.delete(queryWrapper);

                    tMeituanMdOtherMapper.insert(tMeituanMd);
                    System.out.println("解析成功！");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            Thread.sleep(500);
        }
    }




    @Override
    public void gwMedicineListOther() {
      //  List<String> poiGetidList = poiGetidsOther();

        List<String> poiGetidList = tMeituanMdOtherMapper.select1();

        //       List<String> poiGetidList = new ArrayList<>();

//        poiGetidList.add("101888");
//
//        tMeituanWareMapper.delete(null);
        for(String busno:poiGetidList) {
            int offset =1;
            int limit = 200;
            List<TMeituanWareOther> tMeituanWareList = new ArrayList<>();
            int total = gwMedicineListnbOther(busno,Integer.toString(offset),
                    Integer.toString(limit),tMeituanWareList
            );
            System.out.println(total);

            int count = (int) Math.ceil(total/limit);

            for(int i = 1;i<=count;i++){
                System.out.println("======================"+i);
                offset = offset + limit;
                gwMedicineListnbOther(busno,Integer.toString(offset),Integer.toString(limit),tMeituanWareList);
            }

            QueryWrapper<TMeituanWareOther> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shopno",busno);
            tMeituanWareOtherMapper.delete(queryWrapper);
            tMeituanWareOtherService.saveBatch(tMeituanWareList);

        }

        // 获取当前日期的年月日
        LocalDate today = LocalDate.now();

        // 将 LocalDate 转换为 Date 类型，因为数据库通常用 Date 处理
        Date startOfToday = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());

        QueryWrapper<TMeituanWareOther> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("createdate", startOfToday);
        tMeituanWareOtherMapper.delete(queryWrapper);
    }



    //获取门店id
    public List<String> poiGetidsOther(){
        SystemParam systemParam = new SystemParam("122042", "c8c58659a05616b678d29e9765eda629");
        String uri = "/api/v1/poi/getids";
        String requestMethodType = "GET";
        SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = sgOpenGeneralRequest.doRequest();
            String baseString = sgOpenResponse.getBaseString();
            System.out.println(baseString);
            String requestSig = sgOpenResponse.getRequestSig();
            System.out.println(requestSig);
            String requestResult = sgOpenResponse.getRequestResult();
            System.out.println(requestResult);

            ObjectMapper objectMapper = new ObjectMapper();

            // 将JSON字符串解析为Response对象
            Busno response = objectMapper.readValue(requestResult, Busno.class);

            // 输出解析结果
            System.out.println("Result Code: " + response.getResultCode());
            System.out.println("Data:");
            System.out.println(response.getSuccessList().size());
            System.out.println(response.getData().size());
            for (String data : response.getData()) {
                System.out.println(data);
            }
            return response.getSuccessList();
        } catch (SgOpenException var8) {
            var8.printStackTrace();
        } catch (IOException var9) {
            var9.printStackTrace();
        }
        return null;
    }



    //获取第三方门店和美团门店关系
    @Override
    public void poiboundlist() throws InterruptedException {
        List<String> poiGetidList = poiGetids();

        for(String busno:poiGetidList) {

            SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
            String uri = "/api/v1/ecommerce/poi/bound/list";
            String requestMethodType = "GET";
            SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

            Map<String, String> paramMap = new HashMap();
            paramMap.put("app_poi_code", busno);
            paramMap.put("page_num", "1");
            paramMap.put("page_size", "200");

            sgOpenGeneralRequest.setParamMap(paramMap);


            SgOpenResponse sgOpenResponse;
            try {
                sgOpenResponse = sgOpenGeneralRequest.doRequest();
            } catch (SgOpenException var8) {
                var8.printStackTrace();
                return;
            } catch (IOException var9) {
                var9.printStackTrace();
                return;
            }

            String baseString = sgOpenResponse.getBaseString();
            //System.out.println(baseString);
            String requestSig = sgOpenResponse.getRequestSig();
            //System.out.println(requestSig);
            String requestResult = sgOpenResponse.getRequestResult();
            System.out.println("=============================");
            System.out.println(requestResult);
            System.out.println("=============================");

            ObjectMapper objectMapper = new ObjectMapper();
            try {
                PoiboundResult result = objectMapper.readValue(requestResult, PoiboundResult.class);
                // 现在你可以访问解析后的数据了
                System.out.println("Result code: " + result.getResultCode());
                System.out.println("Total count in extra_info: " + result.getExtraInfo().getTotalCount());
                System.out.println("First data poi_name: " + result.getSuccessList().get(0).getWmPoiId());
            } catch (Exception e) {
                e.printStackTrace();
            }

            Thread.sleep(500);


//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                BusnoListResponse response = objectMapper.readValue(requestResult, BusnoListResponse.class);
//
//                List<BusnoDetail> busnoDetailList = response.getSuccessList();
//
//                for (BusnoDetail busnoDetail : busnoDetailList) {
//                    TMeituanMd tMeituanMd = new TMeituanMd();
//                    tMeituanMd.setAddress(busnoDetail.getAddress());
//                    tMeituanMd.setAppid(busnoDetail.getAppId());
//                    tMeituanMd.setApppoicode(busnoDetail.getAppPoiCode());
//                    tMeituanMd.setCityid(busnoDetail.getCityId());
//                    tMeituanMd.setCtime(busnoDetail.getCtime());
//                    tMeituanMd.setInvoicedescription(busnoDetail.getInvoiceDescription());
//                    tMeituanMd.setInvoiceminprice(busnoDetail.getInvoiceMinPrice());
//                    tMeituanMd.setInvoicesupport(busnoDetail.getInvoiceSupport());
//                    tMeituanMd.setIsonline(busnoDetail.getIsOnline());
//                    //tMeituanMd.setLatitude(busnoDetail.getLatitude());
//                    tMeituanMd.setLocationid(busnoDetail.getLocationId());
//                    tMeituanMd.setLogisticscodes(busnoDetail.getLogisticsCodes());
//                    // tMeituanMd.setLongitude(busnoDetail.getLatitude());
//                    tMeituanMd.setName(busnoDetail.getName());
//                    tMeituanMd.setOpenlevel(busnoDetail.getOpenLevel());
//                    tMeituanMd.setPhone(busnoDetail.getPhone());
//                    tMeituanMd.setPicurl(busnoDetail.getPicUrl());
//                    tMeituanMd.setPrebook(busnoDetail.getPreBook());
//                    tMeituanMd.setPrebookmaxdays(busnoDetail.getPreBookMaxDays());
//                    tMeituanMd.setPrebookmindays(busnoDetail.getPreBookMinDays());
//                    tMeituanMd.setPromotioninfo(busnoDetail.getPromotionInfo());
//                    tMeituanMd.setRemark(busnoDetail.getRemark());
//                    tMeituanMd.setShippingfee(busnoDetail.getShippingFee());
//                    tMeituanMd.setShippingtime(busnoDetail.getShippingTime());
//                    tMeituanMd.setStandbytel(busnoDetail.getStandbyTel());
//                    tMeituanMd.setTagname(busnoDetail.getTagName());
//                    tMeituanMd.setTimeselect(busnoDetail.getTimeSelect());
//                    tMeituanMd.setUtime(busnoDetail.getUtime());
//
//                    QueryWrapper<TMeituanMd> queryWrapper = new QueryWrapper<>();
//                    queryWrapper.eq("apppoicode",busnoDetail.getAppPoiCode());
//                    tMeituanMdMapper.delete(queryWrapper);
//
//                    tMeituanMdMapper.insert(tMeituanMd);
//                    System.out.println("解析成功！");
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }
    }

    //查询门店药品折扣商品和爆品商品活动act/retail/discount/list
    @Override
    public void actRetailDiscountList() {

        //List<String> poiGetidList = tMeituanWareMapper.selectTMeituanWareBusno();
       // List<String> poiGetidList = poiGetids();
        List<String> poiGetidList = tMeituanMdMapper.select1();

        //tMeituanMdMapper.selectList()

        //101888
 //       List<String> poiGetidList = new ArrayList<>();
//        poiGetidList.add("101888");

//        actRetailDiscountMapper.delete2();
//
//        actRetailDiscountMapper.delete(null);
//
//        tMeituanWareMapper.delete(null);
        for(String busno:poiGetidList) {

            int offset = 0; // 起始偏移量
            int limit = 200; // 每页查询条数
            int cs = 0;//调用次数
            boolean hasMoreData = true; // 是否还有更多数据
            List<ActRetailDiscount> actRetailDiscountList = new ArrayList<>();

            SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
            ActRetailDiscountListRequest actRetailDiscountListRequest = new ActRetailDiscountListRequest(systemParam);
            actRetailDiscountListRequest.setApp_poi_code(busno);

            while (hasMoreData) {
                System.out.println("起始偏移量：" + offset);
                cs = cs + 1;
                System.out.println("调用次数：" + cs);

                actRetailDiscountListRequest.setOffset(offset);
                actRetailDiscountListRequest.setLimit(limit);
                SgOpenResponse sgOpenResponse;
                try {
                    sgOpenResponse = actRetailDiscountListRequest.doRequest();
                } catch (SgOpenException e) {
                    e.printStackTrace();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
                //发起请求时的sig，用来联系美团员工排查问题时使用
                String requestSig = sgOpenResponse.getRequestSig();
                //请求返回的结果，按照官网的接口文档自行解析即可
                String requestResult = sgOpenResponse.getRequestResult();
                System.out.println(requestResult);
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    ActRetailDiscountParam product = objectMapper.readValue(requestResult, ActRetailDiscountParam.class);
                    // 检查是否有数据
                    if (product.getData().size() > 0) {
                        //处理数据
                        for(ActRetailDiscountItem actRetailDiscountItem:product.getData()){
                            ActRetailDiscount actRetailDiscount = new ActRetailDiscount();
                            actRetailDiscount.setShopNo(busno);
                            actRetailDiscount.setActPrice(actRetailDiscountItem.getAct_price());
                            actRetailDiscount.setAppSpuCode(actRetailDiscountItem.getApp_spu_code());
                            actRetailDiscount.setDiscountCoefficient(actRetailDiscountItem.getDiscount_coefficient());
                            actRetailDiscount.setName(actRetailDiscountItem.getName());
                            actRetailDiscount.setOriginPrice(actRetailDiscountItem.getOrigin_price());
                            actRetailDiscount.setSkuId(actRetailDiscountItem.getSku_id());
                            actRetailDiscountList.add(actRetailDiscount);
                        }
                        offset += limit;
                    } else {
                        // 如果没有数据，停止循环
                        hasMoreData = false;

                        actRetailDiscountMapper.delete2(Integer.parseInt(busno));

//                        QueryWrapper<ActRetailDiscount> queryWrapper = new QueryWrapper<>();
//                        queryWrapper.eq("shop_no",busno);
                        actRetailDiscountMapper.delete(null);
                        actRetailDiscountService.saveBatch(actRetailDiscountList);

                        tMeituanMdMapper.updateFinish(busno);


                        actRetailDiscountMapper.insert2();

                        actRetailDiscountMapper.delete3(Integer.parseInt(busno));

                        actRetailDiscountMapper.insert3();


                    }

//            for (ActRetailDiscountItem item : product.getData()) {
//                System.out.println(item.getAct_price());
//            }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
//        actRetailDiscountMapper.insert2();
    }



    //查询门店药品折扣商品和爆品商品活动act/retail/discount/list
    @Override
    public void actRetailDiscountListOther() {

        //List<String> poiGetidList = tMeituanWareMapper.selectTMeituanWareBusno();
       // List<String> poiGetidList = poiGetidsOther();
        List<String> poiGetidList = tMeituanMdOtherMapper.select1();

        //101888
        //       List<String> poiGetidList = new ArrayList<>();
//        poiGetidList.add("101888");


//
//        tMeituanWareMapper.delete(null);
        for(String busno:poiGetidList) {

            int offset = 0; // 起始偏移量
            int limit = 200; // 每页查询条数
            int cs = 0;//调用次数
            boolean hasMoreData = true; // 是否还有更多数据
            List<ActRetailDiscountOther> actRetailDiscountList = new ArrayList<>();

            SystemParam systemParam = new SystemParam("122042", "c8c58659a05616b678d29e9765eda629");
            ActRetailDiscountListRequest actRetailDiscountListRequest = new ActRetailDiscountListRequest(systemParam);
            actRetailDiscountListRequest.setApp_poi_code(busno);

            while (hasMoreData) {
                System.out.println("起始偏移量：" + offset);
                cs = cs + 1;
                System.out.println("调用次数：" + cs);

                actRetailDiscountListRequest.setOffset(offset);
                actRetailDiscountListRequest.setLimit(limit);
                SgOpenResponse sgOpenResponse;
                try {
                    sgOpenResponse = actRetailDiscountListRequest.doRequest();
                } catch (SgOpenException e) {
                    e.printStackTrace();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
                //发起请求时的sig，用来联系美团员工排查问题时使用
                String requestSig = sgOpenResponse.getRequestSig();
                //请求返回的结果，按照官网的接口文档自行解析即可
                String requestResult = sgOpenResponse.getRequestResult();
                System.out.println(requestResult);
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    ActRetailDiscountParam product = objectMapper.readValue(requestResult, ActRetailDiscountParam.class);
                    // 检查是否有数据
                    if (product.getData().size() > 0) {
                        //处理数据
                        for(ActRetailDiscountItem actRetailDiscountItem:product.getData()){
                            ActRetailDiscountOther actRetailDiscount = new ActRetailDiscountOther();
                            actRetailDiscount.setShopNo(busno);
                            actRetailDiscount.setActPrice(actRetailDiscountItem.getAct_price());
                            actRetailDiscount.setAppSpuCode(actRetailDiscountItem.getApp_spu_code());
                            actRetailDiscount.setDiscountCoefficient(actRetailDiscountItem.getDiscount_coefficient());
                            actRetailDiscount.setName(actRetailDiscountItem.getName());
                            actRetailDiscount.setOriginPrice(actRetailDiscountItem.getOrigin_price());
                            actRetailDiscount.setSkuId(actRetailDiscountItem.getSku_id());
                            actRetailDiscountList.add(actRetailDiscount);
                        }
                        offset += limit;
                    } else {
                        // 如果没有数据，停止循环
                        hasMoreData = false;

                        actRetailDiscountOtherMapper.delete2(Integer.parseInt(busno));

//                        QueryWrapper<ActRetailDiscountOther> queryWrapper = new QueryWrapper<>();
//                        queryWrapper.eq("shop_no",busno);
                        actRetailDiscountOtherMapper.delete(null);
                        actRetailDiscountOtherService.saveBatch(actRetailDiscountList);

                        tMeituanMdOtherMapper.updateFinish(busno);


                        actRetailDiscountOtherMapper.insert2();

                        actRetailDiscountOtherMapper.delete3(Integer.parseInt(busno));

                        actRetailDiscountOtherMapper.insert3();


                    }

//            for (ActRetailDiscountItem item : product.getData()) {
//                System.out.println(item.getAct_price());
//            }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }




    //获取商品详情
    @Override
    public void gwMedicineGet() {
        SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3", "101888");

        //组建请求参数,如有其它参数请补充完整
        GetSpuInfoRequest request = new GetSpuInfoRequest(systemParam);
        request.setApp_poi_code("101888");
        request.setApp_medicine_code("1171751");

        //发起请求
        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = request.doRequest();
        } catch (SgOpenException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        //发起请求时的sig，用来联系美团员工排查问题时使用
        String requestSig = sgOpenResponse.getRequestSig();
        //请求返回的结果，按照官网的接口文档自行解析即可
        String requestResult = sgOpenResponse.getRequestResult();
        System.out.println(requestResult);
    }

    //查询门店内的所有加价购活动信息
    @Override
    public void getRepurchaseList() {
        SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
        ActMarkupRepurchaseListRequest actMarkupRepurchaseListRequest = new ActMarkupRepurchaseListRequest(systemParam);
        actMarkupRepurchaseListRequest.setApp_poi_code("101888");
        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = actMarkupRepurchaseListRequest.doRequest();
        } catch (SgOpenException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        //发起请求时的sig，用来联系美团员工排查问题时使用
        String requestSig = sgOpenResponse.getRequestSig();
        //请求返回的结果，按照官网的接口文档自行解析即可
        String requestResult = sgOpenResponse.getRequestResult();
        System.out.println(requestResult);
    }

    //查询门店内活动信息
    @Override
    public void actAllGetByAppPoiCodeAndType() {
        SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
        String uri = "/api/v1/act/all/get/byAppPoiCodeAndType";
        String requestMethodType = "GET";
        SgOpenGeneralRequest sgOpenGeneralRequest = new SgOpenGeneralRequest(uri, requestMethodType, systemParam);

        Map<String, String> paramMap = new HashMap();
        paramMap.put("app_poi_code", "101888");
        paramMap.put("status", "1");
        paramMap.put("type", "1");

        sgOpenGeneralRequest.setParamMap(paramMap);


        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = sgOpenGeneralRequest.doRequest();
        } catch (SgOpenException var8) {
            var8.printStackTrace();
            return;
        } catch (IOException var9) {
            var9.printStackTrace();
            return;
        }

        String baseString = sgOpenResponse.getBaseString();
        System.out.println(baseString);
        String requestSig = sgOpenResponse.getRequestSig();
        System.out.println(requestSig);
        String requestResult = sgOpenResponse.getRequestResult();
        System.out.println(requestResult);
    }

//查询加价购活动商品明细
    @Override
    public void actMarkupRepurchaseFoodList() {
        SystemParam systemParam = new SystemParam("122041", "05545a1d68106ad8b4148c280914f3f3");
        ActMarkupRepurchaseFoodListRequest actMarkupRepurchaseFoodListRequest = new ActMarkupRepurchaseFoodListRequest(systemParam);
        actMarkupRepurchaseFoodListRequest.setApp_poi_code("101888");
        actMarkupRepurchaseFoodListRequest.setAct_id("1836684851232288800");
        SgOpenResponse sgOpenResponse;
        try {
            sgOpenResponse = actMarkupRepurchaseFoodListRequest.doRequest();
        } catch (SgOpenException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        //发起请求时的sig，用来联系美团员工排查问题时使用
        String requestSig = sgOpenResponse.getRequestSig();
        //请求返回的结果，按照官网的接口文档自行解析即可
        String requestResult = sgOpenResponse.getRequestResult();
        System.out.println(requestResult);
    }

}
